package com.quickcancellation.crm.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mxs
 */
//@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    /**
     * 拦截标注@SaCheckLogin的类或方法，必须登录才能通过校验
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludeList  = new ArrayList();
        excludeList.add("/login");
        excludeList.add("/isLogin");
//        excludeList.add("/getUsernumber");

        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**").excludePathPatterns(excludeList);

        // 路由拦截 根据不同的路由进行拦截
//        registry.addInterceptor(new SaInterceptor(handler -> {
//            // 指定一条 match 规则
//            // 根据路由划分模块，不同模块不同鉴权
//            SaRouter.match("/dictionary/**", r -> StpUtil.checkRole("dictionary"));
//            SaRouter.match("/model/**", r -> StpUtil.checkRole("model"));
//            SaRouter.match("/product/**", r -> StpUtil.checkRole("product"));
//            SaRouter.match("/userinfo/**", r -> StpUtil.checkRole("userInfo"));
//        })).addPathPatterns("/**");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        //解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        //解决knife4j无法访问
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        //解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars");
    }
}
